﻿Imports Hinshelwood.TFSStickyBuddy.Modules.Interfaces
Imports Microsoft.Practices.Composite.Regions
Imports Microsoft.Practices.Composite.Events
Imports Microsoft.Practices.Unity
Imports Microsoft.Practices.Composite.Wpf.Commands
Imports Hinshelwood.TFSStickyBuddy.Infrastructure
Imports Hinshelwood.TFSStickyBuddy.Infrastructure.Events
Imports Infragistics.Windows.DockManager
Imports Microsoft.Practices.Composite.Wpf.Regions
Imports Infragistics.Windows.Ribbon
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Namespace Controllers


    Public Class AreaIterationController
        Implements IAreaIterationController

        Private ReadOnly m_regionManager As IRegionManager
        Private ReadOnly m_eventAggregator As IEventAggregator
        Private ReadOnly m_container As IUnityContainer
        Private ReadOnly m_ContentService As IContentService
        Private ReadOnly m_NavigationService As INavigationService
        Private Shared m_TreeByAreaCommand As DelegateCommand(Of Uri)
        Private Shared m_TreeByIterationCommand As DelegateCommand(Of Uri)

        Private m_TreeByAreaPresenter As ITreeByPresenter
        Private m_TreeByIterationPresenter As ITreeByPresenter

        Private m_SelectedProject As Project

        Public ReadOnly Property TreeByAreaCommand() As Microsoft.Practices.Composite.Wpf.Commands.DelegateCommand(Of System.Uri) Implements Interfaces.IAreaIterationController.TreeByAreaCommand
            Get
                Return m_TreeByAreaCommand
            End Get
        End Property

        Public ReadOnly Property TreeByIterationCommand() As Microsoft.Practices.Composite.Wpf.Commands.DelegateCommand(Of System.Uri) Implements Interfaces.IAreaIterationController.TreeByIterationCommand
            Get
                Return m_TreeByIterationCommand
            End Get
        End Property

        Public Sub New(ByVal regionManager As IRegionManager, _
                       ByVal container As IUnityContainer, _
                       ByVal eventAggregator As IEventAggregator, _
                       ByVal ContentService As IContentService, _
                       ByVal NavigationService As INavigationService)
            m_regionManager = regionManager
            m_eventAggregator = eventAggregator
            m_container = container
            m_ContentService = ContentService
            m_NavigationService = NavigationService
            ' Create Commands
            m_TreeByAreaCommand = New DelegateCommand(Of Uri)(AddressOf OnTreeByAreaExecuted, AddressOf OnTreeByAreaCanExecute)
            m_TreeByIterationCommand = New DelegateCommand(Of Uri)(AddressOf OnTreeByIterationExecuted, AddressOf OnTreeByIterationCanExecute)
            ' Subscribe to Events
            m_eventAggregator.GetEvent(Of TeamProjectSelectionEvent).Subscribe(AddressOf OnTeamProjectSelection)

        End Sub

        Sub OnTeamProjectSelection(ByVal teamProject As Project)
            m_SelectedProject = teamProject
            TreeByAreaCommand.RaiseCanExecuteChanged()
            TreeByIterationCommand.RaiseCanExecuteChanged()
            If Not m_TreeByAreaPresenter Is Nothing Then
                m_TreeByAreaPresenter.View.Model.TeamProject = teamProject
            End If
            If Not m_TreeByIterationPresenter Is Nothing Then
                m_TreeByIterationPresenter.View.Model.TeamProject = teamProject
            End If
        End Sub


        Sub OnTreeByAreaExecuted(ByVal server As Uri)
            m_ContentService.EnsureBaseRegions()
            If m_TreeByAreaPresenter Is Nothing Then
                m_TreeByAreaPresenter = m_container.Resolve(Of ITreeByPresenter)()

            End If
            m_TreeByAreaPresenter.ShowView(Node.TreeType.Area, m_SelectedProject)
        End Sub

        Function OnTreeByAreaCanExecute(ByVal server As Uri) As Boolean
            Return Not m_SelectedProject Is Nothing
        End Function

        Sub OnTreeByIterationExecuted(ByVal server As Uri)
            m_ContentService.EnsureBaseRegions()
            If m_TreeByIterationPresenter Is Nothing Then
                m_TreeByIterationPresenter = m_container.Resolve(Of ITreeByPresenter)()
            End If
            m_TreeByIterationPresenter.ShowView(Node.TreeType.Iteration, m_SelectedProject)
        End Sub

        Function OnTreeByIterationCanExecute(ByVal server As Uri) As Boolean
            Return Not m_SelectedProject Is Nothing
        End Function

    End Class

End Namespace